package main.ressik;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class FoodListController{
	
	private FoodDb fdb;
	
	
	
	
	
	/**
	 * Constructor.
	 */
	public FoodListController(){
		fdb = FoodDb.getInstance();
	}
	
	
	
	
	
	/**
	 * The events that occur when a food in the list of foods is clicked. 
	 * This means that the user tapped one of the food items.  This brings up an alert dialog and
	 * shows the user information regarding that food.  
	 */
	public void listViewClick(long id, Main main, ListView _list){
		ListView list = _list;
    	int newId = (int)id;
    	String food = (String) list.getItemAtPosition(newId);
    	Cursor c = fdb.getSingleFood(food);
    	if (c.moveToNext()){
    		int nameIndex = c.getColumnIndex("name");
    		String name = c.getString(nameIndex);
    		int calIndex = c.getColumnIndex("cals");
    		String cals = c.getString(calIndex);
    		int fatIndex = c.getColumnIndex("fat");
    		String fat = c.getString(fatIndex);
    		int fiberIndex = c.getColumnIndex("fiber");
    		String fiber = c.getString(fiberIndex);
    		int calsInt = Integer.parseInt(cals);
    		int fatInt = Integer.parseInt(fat);
    		int fiberInt = Integer.parseInt(fiber);
    		if (fiberInt > 4){
    			fiberInt = 4;
    		}
    		Integer pointsInt = (calsInt/50) + (fatInt/12) - (fiberInt/5);
    		String points = pointsInt.toString();
    		new AlertDialog.Builder(main).setCancelable(true).setTitle(name).setMessage("Calories: " + cals + " \n" 
    				+ "Fat: " + fat + " \n" 
    				+ "Fiber: " + fiber + "\n"
    				+ "Points: " + points).show();
    	}
    }
	
	
	
	
	
	/**
	 * The events that occur when a food in the list of foods is LONG clicked. 
	 * This means the user tapped and held one of the food items.  It brings up 
	 * another list with options to Delete, or edit the food.
	 */
    @SuppressWarnings("unchecked")
	public void listViewLongClick(final String food, final Main main, final AutoCompleteTextView food_txt, final AutoCompleteTextView cals_txt, final AutoCompleteTextView fat_txt, final AutoCompleteTextView fiber_txt, final Button add_food_btn, final FoodListView flv){
    	final ListView list = new ListView(main);
    	ArrayList<String> menuItems = new ArrayList<String>();
    	menuItems.add(new String("Edit Food"));
    	menuItems.add(new String("Delete Food"));
    	menuItems.add(new String("Add Food to Date"));
    	ListAdapter adapter = new ArrayAdapter(main,android.R.layout.simple_dropdown_item_1line,menuItems);
    	list.setAdapter(adapter);
    	final AlertDialog dialog = new AlertDialog.Builder(main).setTitle(food).setView(list).setInverseBackgroundForced(true).show();
    	//Click the "Delete Food" link
    	list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			public void onItemClick(AdapterView av,View v, int position, long id){
				int newId = (int)id;
				String itemClicked = (String) av.getItemAtPosition(newId);
				if (itemClicked.equals("Delete Food")){
					deleteFoodClick(food, main, flv);
					dialog.hide();
				}
				if (itemClicked.equals("Edit Food")){
					dialog.hide();
					editFoodClick(food, main, food_txt, cals_txt, fat_txt, fiber_txt, add_food_btn);
				}
				if (itemClicked.equals("Add Food to Date")){
					dialog.hide();
					addFoodToDateClick(food, main);
				}
			}
		});
    }
    
    
    
    
    
    /**
     * This is the event that occurs when the user clicks the "Delete Food" option
     * from the listViewLongClick() method.  It removes the food from the DB and the list.
     */
    public void deleteFoodClick(final String food, final Main main, final FoodListView flv){
    	if (fdb.foodExistsInDate(food)){
    		int i = 0;
    		final AlertDialog.Builder d = new AlertDialog.Builder(main).setTitle("Delete Foods in Calendar?");
    		d.setMessage(food + " is currently in your calendar.  if you delete this food, it will also delete all entries in the calendar.");
    		d.setPositiveButton("Delete Food", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface di, int i){
    				fdb.deleteFood(food);
    				Toast.makeText(main, food + " Deleted", Toast.LENGTH_SHORT).show();
    				flv.buildList();
    			}
    		});
    		d.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    			public void onClick(DialogInterface di, int i){
    				i = 1;
    			}
    		});
    		AlertDialog dialog = d.show();
    		if (i==1) dialog.hide();
    	}
    	if (!fdb.foodExistsInDate(food)){
    		fdb.deleteFood(food);
    		Toast.makeText(main, food + " Deleted", Toast.LENGTH_SHORT).show();
    		flv.buildList();
    	}
    	flv.buildList();
    }
    
    
    
    
    
    /**
     * This is the event that occurs when the user clicks the "Edit Food" option
     * from the listViewLongClick() method.  It takes the user back to the "Add Food"
     * tab and pre-populates the fields with information for that food.  They are then
     * able to edit the food.  
     */
    public void editFoodClick(String food, Main main, AutoCompleteTextView food_txt, AutoCompleteTextView cals_txt, AutoCompleteTextView fat_txt, AutoCompleteTextView fiber_txt, Button add_food_btn){
    	main.getTabHost().setCurrentTab(0);
    	Cursor c = fdb.getSingleFood(food);
    	food_txt.setEnabled(false);
    	String name = null;
    	String cals = null;
    	String fat = null;
    	String fiber = null;
    	if (c.moveToNext()){
    		int nameIndex = c.getColumnIndex("name");
    		name = c.getString(nameIndex);
    		int calIndex = c.getColumnIndex("cals");
    		cals = c.getString(calIndex);
    		int fatIndex = c.getColumnIndex("fat");
    		fat = c.getString(fatIndex);
    		int fiberIndex = c.getColumnIndex("fiber");
    		fiber = c.getString(fiberIndex);
    	}
    	if (name == null || cals == null || fat == null || fiber == null){
    		new AlertDialog.Builder(main).setTitle("Error:").setMessage("There was a problem editing your food").show();
    	}
    	food_txt.setText(name);
    	cals_txt.setText(cals);
    	fat_txt.setText(fat);
    	fiber_txt.setText(fiber);
    	add_food_btn.setText("Update Food");
    }
    
    
    
    
    
    /**
     * These are the events that happen when the user clicks to add the food to the date.  
     */
    public void addFoodToDateClick(String food, Main main){
    	DatePicker dp = (DatePicker) main.findViewById(R.id.date_picker);
    	String day = new Integer(dp.getDayOfMonth()).toString();
    	String month = new Integer(dp.getMonth()).toString();
    	String year = new Integer(dp.getYear()).toString();
    	String fullDate = day + month + year;
    	fdb.addFoodToDate(food, fullDate);
    	fdb.getFoodsForDay(fullDate);
    	Toast.makeText(main, food + " Added to Date on Calendar Tab", Toast.LENGTH_SHORT).show();
    }
    
    public ArrayList<String> getFoodsInDb(){
    	return fdb.getFoodsInDb();
    }
    
    
    
}
